package ssl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

public class ComonUtil {

	// private static Logger logger = LoggerFactory.getLogger(CommonUtil.class);
	 
	public static String timeStampToDate(String timestampString, String formats) {
		
       if (StringUtils.isEmpty(formats)){
       	formats = "yyyy-MM-dd HH:mm:ss";
       }
       Long timestamp = Long.parseLong(timestampString);
       String date = new SimpleDateFormat(formats).format(new Date(timestamp));
       
       return date;
       
   }
	/**
	 * 自动增加3个0
	 * @param timestampString
	 * @param formats
	 * @return
	 */
	public static String timeStampToDateExt(String timestampString, String formats) {
		
       if (StringUtils.isEmpty(formats)){
       	formats = "yyyy-MM-dd HH:mm:ss";
       }
       if(StringUtils.isEmpty(timestampString)){
       	return "";
       }
       Long timestamp = Long.parseLong(timestampString)*1000;
       String date = new SimpleDateFormat(formats).format(new Date(timestamp));
       
       return date;
       
   }
	
	public static Long calculateERC20Gasprice(Long fee,Long gas){
		if(fee != null && gas != null && gas>0){
			BigDecimal feeValue = new BigDecimal(fee.toString());
			BigDecimal gasValue = new BigDecimal(gas.toString());
			BigDecimal unit = new BigDecimal("10000000000");
			return feeValue.divide(gasValue,10,RoundingMode.DOWN).multiply(unit).longValue();
		}
		return null ;
	}
	
	public static String convertAmountToShow(String fee) {
		// TODO fee  需要为long的字符
		if(StringUtils.isNotEmpty(fee)){
			fee=truncateDouble(fee, 8);
			//10的8次方
			BigDecimal convertNumber= new BigDecimal(100000000);
			BigDecimal amountNum = new BigDecimal(fee);
			return amountNum.divide(convertNumber).toPlainString();
		}
		return "";
	}
	/**
	 * 
	 * @param fee
	 * @return
	 */
	public static String convertLongToOrignAmount(Long amount) {
		// TODO fee  需要为long的字符
		if(amount != null){
			//10的8次方
			BigDecimal convertNumber= new BigDecimal(100000000);
			BigDecimal amountNum = new BigDecimal(amount.toString());
			return amountNum.divide(convertNumber).toPlainString();
		}
		return "";
	}
	
	 public static String truncateDouble(String doubleStr,int dotLen){
	    	StringBuffer sb = new StringBuffer();
	    	if(doubleStr != null && doubleStr.trim().length()>0){
	    		int len = doubleStr.indexOf(".");
	    		if(len>0){
	    			String[] numberSplit = doubleStr.split("\\.");
					String subStr = numberSplit[1];
					if(subStr.length()>dotLen){
						subStr = subStr.substring(0,dotLen);
					}
					
					sb.append(numberSplit[0]).append(".").append(subStr);
	    		}
	    	}
	    	
	    	if(sb.length()<=0){
	    		sb.append(doubleStr);
	    	}
	    	
	    	return sb.toString() ;
	    }
	 
	 /**
	  * matrix  , 拷贝到 resource下 不行, 调整后 可以了
	  * @return
	  */
	 public static InputStream getHttpsCa(){
		 try{
			 /*InputStream inputStream = CommonUtil.class.getResourceAsStream("matrix");
			 
			 System.out.println(inputStream);
			 InputStream inputStream1 = CommonUtil.class.getResourceAsStream("/matrix");
			 System.out.println(inputStream1);
			 return inputStream1 ;*/
			 
			 InputStream inputStream = ComonUtil.class.getResourceAsStream("/matrix");
			 //logger.debug("--->getHttpsCa matrix  read , inputStream:{} ", inputStream);
			 return inputStream ;
			 /*String path = CommonUtil.class.getResource("").getPath() ;
			 File file = new File(path+File.separator+"matrix");
			 logger.debug("--->getHttpsCa matrix path is  , path:{} , file:{} ", path,file.getAbsoluteFile());*/
			 
			/* System.out.println(CommonUtil.class.getResource("/").getPath());
			 System.out.println(CommonUtil.class.getResource("").getPath());*/
			 
//			 InputStream in = new FileInputStream(file);
//			 return in ;
			/* URL resource = CommonUtil.class.getResource("matrix");
			//System.out.println(CommonUtil.class.getClassLoader().getResource(name));
			 //InputStream inputStream = CommonUtil.class.getResourceAsStream("/txt");
			 //URL resource = CommonUtil.class.getResource("/matrix");
			 System.out.println("resource.toString():"+resource.toString());
			 System.out.println("resource.getFile()"+resource.getFile());
			 System.out.println("resource.getPath()"+resource.getPath());
//           File file = new File("E:\\Program Files\\Java\\jdk1.8.0_162\\jre\\lib\\security\\matrix");
//           File file = new File("E:\\code\\inchat_parent_develop_2\\chips-gateway\\chips-gateway-client\\chips-gateway-client-wallet\\target\\classes\\com\\chips\\gateway\\client\\wallet\\utils\\matrix");
//             File file = new File("E:\\document\\matrix");
           File file = new File("matrix");
//           System.out.println("Loading KeyStore " + file + "...");
//           InputStream in = new FileInputStream(file);
			 
			 
			 File file = new File(resource.getFile());
			 
			 System.out.println(file.getAbsolutePath());
			 
			 InputStream in = new FileInputStream(file);
			 return in ;*/
//			 return null ;
		 }catch(Exception e){
			// logger.error("--->getHttpsCa matrix can not read , errmsg:{} ", e.getMessage());
		 }
		 return null ;
	 }
	 
	 /**
	    * 读取 InputStream 到 String字符串中
	    *//*
	   public static String readStream(InputStream in) {
	       try {
	           //<1>创建字节数组输出流，用来输出读取到的内容
	           ByteArrayOutputStream baos = new ByteArrayOutputStream();
	           //<2>创建缓存大小
	           byte[] buffer = new byte[1024]; // 1KB
	           //每次读取到内容的长度
	           int len = -1;
	           //<3>开始读取输入流中的内容
	           while ((len = in.read(buffer)) != -1) { //当等于-1说明没有数据可以读取了
	               baos.write(buffer, 0, len);   //把读取到的内容写到输出流中
	           }
	           //<4> 把字节数组转换为字符串
	           String content = baos.toString();
	           //<5>关闭输入流和输出流
	           in.close();
	           baos.close();
	           //<6>返回字符串结果
	           return content;
	       } catch (Exception e) {
	           e.printStackTrace();
	           return  e.getMessage();
	       }
	   }*/
	 
	 public static void main(String[] args) {
		 String timeStampToDateExt = timeStampToDateExt("1430433028","");
		 System.out.println("timeStampToDateExt:"+timeStampToDateExt);
//		 System.out.println(readStream(getHttpsCa()));
		 System.out.println(getHttpsCa());
	}
}
